package com.kye.express.business.tcl.detail;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.tcl.RequestTCLGoodsDetails;
import com.kye.model.bean.tcl.ResponseTCLGoodsDetailsBean;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.TCLDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public interface TCLDetailContract {

    interface TCLDetailView extends IView {
        void onSuccessLoadData(OpenApiResult<ResponseTCLGoodsDetailsBean> result);

        void onApiError(OpenApiResult<ResponseTCLGoodsDetailsBean> result, int code, String msg);

        void onOtherError(Throwable e);

        void onLoadDataUser(User user);
    }

    class TCLDetailPresenter extends BasePresenter<TCLDetailView> {

        private TCLDataSource tclDataSource;

        TCLDetailPresenter() {
            tclDataSource = new TCLDataSource();
        }

        @Override
        public void bind(TCLDetailView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onLoadDataUser(user);
                        }
                    });
        }

        void requestToTCLGoodsDetail(String phone, String productId) {
            tclDataSource.getTCLGoodsDetail(new RequestTCLGoodsDetails(phone, productId))
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResponseTCLGoodsDetailsBean>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResponseTCLGoodsDetailsBean> result, ResponseTCLGoodsDetailsBean responseTCLGoodsDetailsBean) {
                            getView().onSuccessLoadData(result);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResponseTCLGoodsDetailsBean> result, int code, String msg) {
                            getView().onApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(e);
                        }
                    });
        }

    }
}
